package com.leetcode.No0053;

import org.junit.Test;

public class Solution {
    public int maxSubArray(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        int sum = 0, sum2;
        for (int num : nums) {
            sum += num;
        }
        sum2 = sum;
        int result = sum;
        while (i < j) {
            int tmp;
            if (nums[i] <= nums[j]) {
                tmp = nums[i];
                ++i;
            } else {
                tmp = nums[j];
                --j;
            }
            sum -= tmp;
            result = Math.max(result, sum);
        }
        i = 0;
        j = nums.length - 1;
        while (i < j) {
            int tmp;
            if (nums[i] < nums[j]) {
                tmp = nums[i];
                ++i;
            } else {
                tmp = nums[j];
                --j;
            }
            sum2 -= tmp;
            result = Math.max(result, sum2);
        }
        return result;
    }

    @Test
    public void test() {
        int[] nums = {1,2,-1,-2,2,1,-2,1,4,-5,4};
        System.out.println(maxSubArray(nums));
    }

}
